package game.module.hall.processor;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.ddz.bean.DdzRoom;
import game.module.ddz.logic.DdzDataManager;
import game.module.hall.logic.CheatManager;
import game.module.hall.logic.QipaiConstants;
import game.module.hall.logic.QipaiDataManager;
import game.module.mj.bean.MjRoom;
import game.module.mj.logic.MjDataManager;
import game.module.nn.bean.NnRoom;
import game.module.nn.logic.NnDataManager;
import game.module.qzmj.bean.QzmjRoom;
import game.module.qzmj.logic.QzmjDataManager;
import game.module.sss.bean.SssRoom;
import game.module.sss.logic.SssDataManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.C2SFastJoinRoomCheck;
import lua.LuaMessageHall.S2CFastJoinRoomCheck;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SFastJoinRoomCheck.id, accessLimit = 500)
public class FastJoinRoomCheckProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(FastJoinRoomCheckProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SFastJoinRoomCheck reqMsg = C2SFastJoinRoomCheck.parse(request);
		String room_id = reqMsg.room_id;
		int playerId = hero.getId();
		logger.info("fast join room check,playeyId={},room_id={}", playerId, room_id);
		// 是否在房间中
		boolean isInDdzRoom = DdzDataManager.getInstance().isPlayerInRoom(playerId);
		boolean isInNnRoom = NnDataManager.getInstance().isPlayerInRoom(playerId);
		boolean isInSssRoom = SssDataManager.getInstance().isPlayerInRoom(playerId);
		boolean isInMjRoom = MjDataManager.getInstance().isPlayerInRoom(playerId);
		boolean isInQzmjRoom = QzmjDataManager.getInstance().isPlayerInRoom(playerId);
		if (isInDdzRoom || isInNnRoom || isInSssRoom || isInMjRoom || isInQzmjRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(102);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		// is fang not exist
		Byte roomType = QipaiDataManager.getInstance().getRoomType(room_id);
		if (roomType == null) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			hero.writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		switch (roomType) {
		case QipaiConstants.ROOM_TYPE_DDZ:
			DdzRoom ddzRoom = DdzDataManager.getInstance().getDdzRoom(room_id);

			// 人数已满
			if (ddzRoom.getPlayerList().size() >= 3) {
				S2ErrorCode respMsg = new S2ErrorCode(108);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			break;
		case QipaiConstants.ROOM_TYPE_NN:
			NnRoom nnRoom = NnDataManager.getInstance().getNnRoom(room_id);
			int fangka = hero.getPlayerBean().getFang_ka();
			int fangka_cost = nnRoom.getRoomfee();
			if (nnRoom.getRoomFeeType() == 2 && fangka < fangka_cost)// 赢家收费
			{
				S2ErrorCode respMsg = new S2ErrorCode(103);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (nnRoom.getRoomFeeType() == 1 && fangka < fangka_cost / 2) {// AA
				S2ErrorCode respMsg = new S2ErrorCode(103);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (nnRoom.getIs_jindou() == 1 && hero.getPlayerBean().getJin_dou() < nnRoom.getJindou() * nnRoom.getRoundCount() * 5) {// 金豆房间
				S2ErrorCode respMsg = new S2ErrorCode(133);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			// 人数已满
			if (nnRoom.getPlayerList().size() >= 5) {
				S2ErrorCode respMsg = new S2ErrorCode(109);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (CheatManager.getInstance().checkCheatAndPush(nnRoom.getPrevent_cheat(), nnRoom.getIps(), hero)) {
				return;
			}
			break;
		case QipaiConstants.ROOM_TYPE_SSS:
			SssRoom sssRoom = SssDataManager.getInstance().getSssRoom(room_id);
			int fangk = hero.getPlayerBean().getFang_ka();
			int fangka_cos = sssRoom.getRoomFee();
			if (sssRoom.getIs_jindou() == 1 && hero.getPlayerBean().getJin_dou() < sssRoom.getJindou() * sssRoom.getRoundCount() * 5) {// 金豆房间
				S2ErrorCode respMsg = new S2ErrorCode(133);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (sssRoom.getRoomFeeType() == 2 && fangk < fangka_cos)// 赢家收费
			{
				S2ErrorCode respMsg = new S2ErrorCode(103);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (sssRoom.getRoomFeeType() == 1 && fangk < fangka_cos / 2) {// AA
				S2ErrorCode respMsg = new S2ErrorCode(103);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			// 人数已满
			if (sssRoom.getPlayerList().size() >= 4) {
				S2ErrorCode respMsg = new S2ErrorCode(110);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (CheatManager.getInstance().checkCheatAndPush(sssRoom.getPrevent_cheat(), sssRoom.getIps(), hero)) {
				return;
			}
			break;
		case QipaiConstants.ROOM_TYPE_MJ:
			MjRoom mjRoom = MjDataManager.getInstance().getMjRoom(room_id);

			if (CheatManager.getInstance().checkCheatAndPush(mjRoom.getPrevent_cheat(), mjRoom.getIps(), hero)) {
				return;
			}
			// 人数已满
			if (mjRoom.getPlayerList().size() >= 4) {
				S2ErrorCode respMsg = new S2ErrorCode(111);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			break;
		case QipaiConstants.ROOM_TYPE_MJ_QZ:
			QzmjRoom qzmjRoom = QzmjDataManager.getInstance().getMjRoom(room_id);
			int fang = hero.getPlayerBean().getFang_ka();
			int fangka_ = qzmjRoom.getFee();
			if (qzmjRoom.getCost_type() == 2 && fang < fangka_)// 赢家收费
			{
				S2ErrorCode respMsg = new S2ErrorCode(103);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			if (qzmjRoom.getCost_type() == 1 && fang < fangka_ / 4) {// AA
				S2ErrorCode respMsg = new S2ErrorCode(103);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}

			if (CheatManager.getInstance().checkCheatAndPush(qzmjRoom.getPrevent_cheat(), qzmjRoom.getIps(), hero)) {
				return;
			}
			// 人数已满
			if (qzmjRoom.getPlayerList().size() >= 4) {
				S2ErrorCode respMsg = new S2ErrorCode(111);
				hero.writeAndFlush(respMsg.build(hero.alloc()));
				return;
			}
			break;
		default:
			break;
		}
		// ret
		S2CFastJoinRoomCheck respMsg = new S2CFastJoinRoomCheck(room_id, roomType);
		hero.writeAndFlush(respMsg.build(hero.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

	public String getIpAddr(String ipAddrRaw) {
		String retStr = ipAddrRaw;
		int fenIndex = ipAddrRaw.indexOf(":");
		if (fenIndex >= 0) {
			retStr = retStr.substring(0, fenIndex);
		}
		if (retStr.startsWith("/")) {
			retStr = retStr.substring(1);
		}
		return retStr;
	}

}
